import { Toast } from 'mint-ui';
import Api from './../api/index';
export default {
  back(n){
    n = n||-1;
    this.$router.go(n);
  },
  DATEPICKERSTARTDATE:'2018/1/1',
  DATEPICKERENDDATE:'2018/12/12',
  PHOTOLIMIT:1000,
  toFormat(Date,splitStr='/',insteadStr='-'){
    return Date.split(splitStr).join(insteadStr);
  },
  deepCopy(obj,fn){
    for(let key in obj){
      if(typeof obj[key] == 'object'){
        this.deepCopy(obj[key],fn);
      }else{
        fn(key,obj[key]);
      }
    }
  },
  each(arr,fn){
    const len = arr.length;
    let i = 0;
    while (i<len){
      fn(arr[i],i);
      i++;
    }
  },
  checkFiles:{
    length(file){
      if(file){
        return true;
      }else{
        Toast({
          message: '您没有选择任何文件',
          position: 'bottom',
          duration: 3000
        });
        return false;
      }
    },
    type(file){
      const reg = /image/;
      if(reg.test(file)){
        return true;
      }else{
        Toast({
          message: '您选择的不是图片类型，请重新选择',
          position: 'bottom',
          duration: 3000
        });
        return false;
      }
    },
    size(file){
      if(file  < 4*1024*1024){
        return true;
      }else{
        Toast({
          message: '您选择的文件大于4M，请重新选择',
          position: 'bottom',
          duration: 3000
        });
        return false;
      }
    }
  },
  test(file){
    for(let key in file){
      if(!this['checkFiles'][key]){continue;}
      if(!this['checkFiles'][key](file[key])){return false;}
    }
  },
  fileReader(fileVal,fn){
    const reader=new FileReader();
    reader.readAsDataURL(fileVal);
    reader.onload = function () {
      fn(reader.result);
    }
  },
  getFiles(el,file,fn){
    if(this.test(file) == false){return;}
    let tempFile = file;
    console.log(tempFile);
    for(let item in tempFile){
      if(item < this.PHOTOLIMIT){
        const fileVal = tempFile[item];
        if(this.test(fileVal) == false){return;}
        if(typeof fileVal == 'object'){this.fileReader(fileVal,fn);}
      }else if(item > this.PHOTOLIMIT){
        Toast({
          message: '您选择的文件大于'+this.PHOTOLIMIT+'张，仅保留前'+this.PHOTOLIMIT+'张',
          position: 'bottom',
          duration: 3000
        });
        break;
      }
    }
    el.currentTarget.value = '';
  },
  throttle(fn,time){
    let timer = null;
    return function () {
      clearTimeout(timer);
      const _this = this,
        arg = arguments;
      timer = setTimeout(function(){
        fn.apply(_this,arg);
      },time);
    }
  },
  OK:'1',
  ERROR:'0',
  repeatCode:'-1',
  token:sessionStorage.getItem('token'),
  apiFrom:'zgtcbhtml',
  dataConcat(data) {
    const commonData = {
      apiFrom:this.apiFrom,
      token:'',
      rentId:'9',
      regionId:'360000',
      platform:3
    };
    const _obj = Object.assign({},commonData,JSON.parse(sessionStorage.getItem('lcInfo')));
    return Object.assign({},_obj,data);
  },
  /*statusAry(res){
    let ary = [];
    if(res.s_status == 0){

    }else if(res.s_status == 3){
        ary.push('已取消');
        return ary;
    }
    if(res.bet_number_status == 0 || res.bet_number_status == 2){
      ary.push('待出票');
    }else if(res.bonus_flag == 0){
      if(res.bussback_status == 0){
        ary.push('待反馈');
      }else{
        ary.push('已反馈');
      }
      ary.push('待开奖');
    }else if(res.bonus_flag == 2){
      ary.push('未中奖');
    }else if(res.bonus_back ==0 || res.bonus_back ==2){
      ary.push('待返奖');
    }
    return ary;
  },*/
  statusAry(res){
    let ary = [];
    switch (res.order_status){
        case 2:
          ary.push('已取消');
          break;
        case 3:
            ary.push('已拒绝');
            break;
        case 4:
            ary.push('待出票');
            break;
        case 5:
            if(res.bussback_status == 1){
              ary.push('已反馈');
            }else{
              ary.push('待反馈');
            }
            ary.push('待开奖');
            break;
        case 6:
            ary.push('待返奖');
            break;
        case 7:
            ary.push('未中奖');
            break;
        case 8:
            ary.push('已返奖');
            break;
    }
    return ary;
  },
  addZero(str){
    str += '';
    if(str.length == 1){
      str = '0'+str;
    }
    return str;
  },
  getTime(stamp){
    let d = new Date(stamp*1000);
    return d.getFullYear()+'-'+(d.getMonth()+1)+'-'+d.getDate() + ' ' + this.addZero(d.getHours())+':' + this.addZero(d.getMinutes());
  },
  getDay(stamp){
    let d = new  Date(stamp*1000);
    return '周' + '日一二三四五六'[d.getDay()]+' '+this.addZero(d.getHours())+':'+this.addZero(d.getMinutes());
  },
  sortArr(arr,status){
    for(let i=0;i<arr.length-1;i++){
      for(let j=0;j<arr.length-1-i;j++){
        if((arr[j][status]*1) < (arr[j+1][status]*1)){
          [arr[j],arr[j+1]] = [arr[j+1],arr[j]];
        }
      }
    }
    return arr;
  },
  getData(status,page,order,order_id){
    page = page || 1;
    return Api.list({status,page,order,order_id});
  },
  loadTop() {
    // 加载更多数据
    this.allLoaded = false;
    this.page = 1;
    const _this = this;
    _this.getData().then(function (data){
      _this.success.call(_this,data,function(){
        _this.$refs.loadmore.onTopLoaded();
      });
    });
  },
  loadBottom(){
    // 加载更多数据
    const _this = this;
    this.getData().then(function(data){
        _this.success.call(_this,data,function(){
            _this.$refs.loadmore.onBottomLoaded();
      });
    });
  },
  mounted(){
    const _this = this;
    this.allLoaded = false;
    this.page = 1;
    this.getData().then(function (data){
      _this.success.call(_this,data,function () {});
    });
  },
  query(url) {
    var result = {};
    var query = url.split("?")[1];
    var queryArr = query.split("&");
    queryArr.forEach(function(item){
      var key = item.split("=")[0];
      var value = item.split("=")[1];
      result[key] = value;
    });
    return result;
  },
  ziDianXu(obj){
    if(!obj){return};
    var b = Object.keys(obj).sort(),
      res = {},
      url = [];
    for(var i = 0;i<b.length;i++){
      var $key = b[i];
      res[$key] = obj[$key];
      url.push($key+'='+obj[$key]);
    }
    return url.join('&');
  },
  isIos(){
    let u =navigator.userAgent;
    if(u.indexOf('Android') > -1 || u.indexOf('Adr') > -1)return 'android';
    if(!!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/))return 'IOS';
    return 'other';
  },
  getPublicData(fn){
    if(this.isIos() == 'android'){
      window.newtcb.getCommonData('1234');
    }else if(this.isIos() == 'IOS'){
      window.webkit.messageHandlers.getCommonData.postMessage('function (data) {window.getPublicData(data,'+JSON.stringify(fn)+')}');
    }else{
      Toast({
        message: '请在app中打开',
        position: 'bottom',
        duration: 3000
      })
    }
  },
  callBack(data,fn){
    if((data && data.code) == this.OK){
      fn&&fn(data.result);
    }else if((data && data.code) == this.ERROR){
      Toast.info(data.msg,3);
    }else{
      Toast.info('网络异常或网络请求超时，请稍后重试!',3);
    }
  },
}











































